package com.qqt.csr.im.assembler;

import cn.hutool.json.JSONUtil;
import com.qqt.csr.common.acl.aics.resp.CsMemberInfoDTO;
import com.qqt.csr.common.session.SessionContextHolder;
import com.qqt.csr.common.utils.SnowflakeUtil;
import com.qqt.csr.im.entity.SensitiveWordRepo;
import com.qqt.csr.im.enums.EnableEnum;
import com.qqt.csr.im.enums.SensitiveWordRepoType;
import com.qqt.csr.im.vo.req.SensitiveWordRepoAddReqVO;
import com.qqt.csr.im.vo.req.SensitiveWordRepoUpdateReqVO;
import com.qqt.csr.im.vo.resp.SensitiveWordRepoRespVO;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: yang hao
 * @Date: 2025/1/14 15:47
 * @Description: 敏感词库 装配器
 **/
public class SensitiveWordRepoAssembler {

    /**
     * 敏感词库新增请求 转换为 敏感词库实体
     * @param reqVO 敏感词库新增请求
     * @return 敏感词库实体
     */
    public static SensitiveWordRepo convert(SensitiveWordRepoAddReqVO reqVO) {
        String currentTenantId = SessionContextHolder.getHeaderInfo().getTenantId();
        Long creatorId = SessionContextHolder.getHeaderInfo().getUserId();
        List<Integer> types = reqVO.getTypes().stream().map(SensitiveWordRepoType::getCode).toList();
        String scope = JSONUtil.toJsonStr(types);
        return SensitiveWordRepo.builder()
                .id(SnowflakeUtil.nextId())
                .desc(reqVO.getDesc())
                .name(reqVO.getName())
                .enable(EnableEnum.DISABLE.getCode())
                .scope(scope)
                .creatorId(creatorId)
                .tenantId(currentTenantId)
                .build();
    }

    public static SensitiveWordRepo convert(String tenantId,Long creatorId,String repoName) {
        List<Integer> types = Arrays.stream(SensitiveWordRepoType.values()).map(SensitiveWordRepoType::getCode).collect(Collectors.toList());
        String scope = JSONUtil.toJsonStr(types);
        return SensitiveWordRepo.builder()
                .id(SnowflakeUtil.nextId())
                .desc(StringUtils.EMPTY)
                .name(repoName)
                .enable(EnableEnum.DISABLE.getCode())
                .scope(scope)
                .creatorId(creatorId)
                .tenantId(tenantId)
                .createTime(new Date())
                .updateTime(new Date())
                .build();
    }

    /**
     * 敏感词库修改请求 转换为 敏感词库实体
     * @param reqVO 敏感词库修改请求
     * @return 敏感词库实体
     */
    public static SensitiveWordRepo convert(SensitiveWordRepoUpdateReqVO reqVO) {
        SensitiveWordRepo repo = SensitiveWordRepo.builder()
                .id(reqVO.getId())
                .desc(reqVO.getDesc())
                .build();
        if (reqVO.getEnable() != null) {
            repo.setEnable(reqVO.getEnable().getCode());
        }
        if (reqVO.getTypes() != null && !reqVO.getTypes().isEmpty()) {
            List<Integer> types = reqVO.getTypes().stream().map(SensitiveWordRepoType::getCode).toList();
            repo.setScope(JSONUtil.toJsonStr(types));
        }
        return repo;
    }

    /**
     * 敏感词库实体 转换为 敏感词库响应
     * @param repo 敏感词库实体
     * @param wordCount 敏感词数量
     * @return 敏感词库响应
     */
    public static SensitiveWordRepoRespVO convert(SensitiveWordRepo repo, Long wordCount, Map<Long, CsMemberInfoDTO> memberInfo) {
        SensitiveWordRepoRespVO respVO = SensitiveWordRepoRespVO.builder()
                .id(repo.getId())
                .name(repo.getName())
                .desc(repo.getDesc())
                .enable(EnableEnum.match(repo.getEnable()))
                .createTime(repo.getCreateTime())
                .updateTime(repo.getUpdateTime())
                .wordCount(wordCount == null ? 0 : wordCount)
                .build();
        if (memberInfo.get(repo.getCreatorId()) != null) {
            respVO.setCreatorName(memberInfo.get(repo.getCreatorId()).getNickName());
        }
        List<Integer> typeIds = JSONUtil.parseArray(repo.getScope()).toList(Integer.class);
        if (typeIds != null && !typeIds.isEmpty()) {
            List<SensitiveWordRepoType> types = typeIds.stream().map(SensitiveWordRepoType::match).toList();
            respVO.setTypes(types);
        }
        return respVO;
    }

    /**
     * 敏感词库实体集合 转换为 敏感词库响应集合
     * @param repos 敏感词库实体集合
     * @param wordCounts 敏感词数量集合
     * @return 敏感词库响应集合
     */
    public static List<SensitiveWordRepoRespVO> convert(List<SensitiveWordRepo> repos, Map<Long, Long> wordCounts, Map<Long, CsMemberInfoDTO> memberInfo) {
        if (repos == null || repos.isEmpty()) {
            return null;
        }
        return repos.stream()
                .map(repo -> convert(repo, wordCounts.get(repo.getId()), memberInfo))
                .collect(Collectors.toList());
    }

}
